#! /bin/bash
#author:	weskiller	2016-10-07
#decsription:auto reconnect vpn
GW_NAME=(online phdev)
online_gw=192.168.20.102
phdev_gw=192.168.60.41
declare -i loss
while :;do
	for i in ${GW_NAME[@]};do
		gateway=`eval echo \$\{${i}_gw\}`
		netmask=${gateway%.*}.0
		loss=`ping -c 5 ${gateway} |grep -Po "\d+(?=% packet loss)" 2>/dev/null`
		if [ ${loss} -gt 60 ];then
			poff $i >/dev/null  2>&1
			echo "`date +%F\ %T`: disconnect $i."
			pon $i >/dev/null  2>&1
			until ifconfig |grep -Pq "${gateway}";do
				sleep 1
			done
			ip route add ${netmask}/24 via ${gateway} && echo "`date +%F\ %T`: reconnect $i."
		fi
	done
done
